import json
from typing import List, Dict, Any
from classes.PageElement import PageElement
from classes.TestStep import TestStep
import json


class TestCase:
    """测试用例管理类，包含步骤和元素"""
    def __init__(self, name: str, browser: str = "chrome", headless: bool = False, keep_browser_open: bool = False):
        self.name = name
        self.steps: List[TestStep] = []
        self.elements: Dict[str, PageElement] = {}
        self.browser = browser
        self.headless = headless
        self.keep_browser_open = keep_browser_open

    def add_element(self, element: PageElement):
        self.elements[element.name] = element

    def add_step(self, step: TestStep):
        self.steps.append(step)

    def to_json(self) -> str:
        data = {
            "steps": [step.to_dict() for step in self.steps],
            "browser": self.browser,
            "headless": self.headless,
            "keep_browser_open": self.keep_browser_open
        }
        return json.dumps(data, indent=2)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "steps": [step.to_dict() for step in self.steps],
            "browser": self.browser,
            "headless": self.headless,
            "keep_browser_open": self.keep_browser_open
        }